#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QTextCursor>
#include <QString>
#include <QDebug>
#include <stdio.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton_ok, SIGNAL(clicked()), this, SLOT(mySlot()));
    connect(ui->pushButton_over, SIGNAL(clicked()), this, SLOT(cominit_over()));
    connect(ui->pushButton_tx, SIGNAL(clicked()), this, SLOT(cominit()));
    connect(ui->pushButton_alsa, SIGNAL(clicked()), this, SLOT(pushButton_alsa_ok()));
    connect(ui->pushButton_alsa_over, SIGNAL(clicked()), this, SLOT(pushButton_alsa_over()));
     MainWindow::init_statusBar();

}

MainWindow::~MainWindow()
{
    delete ui;

}

void MainWindow::mySlot()
{
// ui->textEdit

    QFile file("/home/xiao/make.txt"); //文件路径
    QString line;//定义行
    QTextStream in(&file);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
          qDebug()<<"Open file failure : usr_munual"<<file.fileName();
    }
    qDebug()<<"Open file succussfule ....."<<file.fileName();
    unsigned static int n = 0;
    while(!in.atEnd())
    {
        line = in.readLine(); //读入一行数据
        ui->textEdit->append(line);
        qDebug()<<"Screen : "<<n++; //只是为了记录一下读了多少行，呵呵
     }
}
 int count=0;
 int ttime =0 ;
 int  prot= 0;
 char ip_string[40];

 void MainWindow::init_statusBar()
 {
 QStatusBar* bar = ui->statusBar; //获取状态栏
 first_statusLabel = new QLabel; //新建标签
 first_statusLabel->setMinimumSize(150,20); //设置标签最小尺寸
 first_statusLabel->setFrameShape(QFrame::WinPanel); //设置标签形状
 first_statusLabel->setFrameShadow(QFrame::Sunken); //设置标签阴影
 first_statusLabel->setAlignment(Qt::AlignHCenter);

 second_statusLabel = new QLabel;
 second_statusLabel->setMinimumSize(150,20);
 second_statusLabel->setFrameShape(QFrame::WinPanel);
 second_statusLabel->setFrameShadow(QFrame::Sunken);

 bar->addWidget(first_statusLabel);
 bar->addWidget(second_statusLabel,1); // 占满整个窗口
 first_statusLabel->setText(tr("stat")); //初始化内容
 second_statusLabel->setText(tr("kai!"));
 }

void MainWindow:: cominit(void)
{
    QString str = ui->lineEdit_ip->text();
    QString protc = ui->lineEdit_prot->text();
    strcpy(ip_string,str.toAscii().constData());
    ui->textEdit_udp->append("start udp_tx");
    str = ui->lineEdit_cont->text();
    count = str.toInt();
    str = ui->lineEdit_time->text();
    ttime = str.toInt();
    prot = protc.toInt();
    thread.start();
}

void MainWindow:: cominit_over(void)
{
    count = 0;
    ui->textEdit_udp->append("over tx");
}


QUdpSocket  *udpSocket;   //套接字对象

void MyThread::Send()
{
 char information[1024]="123456789";
 udpSocket = new QUdpSocket();
 while(count--)
 {
  udpSocket->writeDatagram(information,sizeof(information),QHostAddress(ip_string),prot);

  msleep(ttime); //发送时间好秒为
  }
  delete udpSocket;
}

void MyThread::run() //运行进程
{
    Send();
}
